#include "BlockQueue.hpp"
#include "Task.hpp"
#include <unistd.h>
#include <stdlib.h>

void *Productor(void *args)
{
    BlockQueue<Task> *bq = (BlockQueue<Task> *)args;

    while (true)
    {
        // static int t = 1; // 任务
        int x = rand() % 10;
        usleep(1000);
        int y = rand() % 10;
        Task t(x, y);
        bq->push(t);
        std::cout << "生产成功：" << t._x << " + " << t._y << std::endl;
        sleep(1);
    }

    return nullptr;
}

void *Consumer(void *args)
{
    BlockQueue<Task> *bq = (BlockQueue<Task> *)args;

    while (true)
    {
        // sleep(1);
        // int t; // 任务
        Task t;
        bq->pop(&t);
        std::cout << "消费成功：" << t._x << " + " << t._y << " = " << t() << std::endl;
    }

    return nullptr;
}

int main()
{
    srand((unsigned int)time(nullptr));

    // 创建一个阻塞队列
    BlockQueue<Task> *bq = new BlockQueue<Task>();

    pthread_t cid, pid; // 消费者id和生产者id
    pthread_create(&pid, nullptr, Productor, (void *)bq);
    pthread_create(&cid, nullptr, Consumer, (void *)bq);

    pthread_join(cid, nullptr);
    pthread_join(pid, nullptr);

    delete bq;

    return 0;
}